import { workflowHttpRequest as request } from '@/utils/http';
import type { GetMyAuditPageListInput, GetMyAuditPageListOutput, MyWorkflowListOutput, StartWorkflowInput, WorkflowAuditDto, WorkflowAuditInput, WorkflowDto, WorkflowListInput, WorkflowListOutput } from './type';
import { getApiUrl, jsonToUrlParam } from '@/utils/urlSearchParams';
import type { PagedResultDto } from '../../type';

// Api地址
enum API {
    DEFAULT_URL = 'api/workflow',
    WORKFLOW_AUDIT_URL = 'api/workflow/audit',
}

// 获取所有分组流程
export const getAllWithGroupAsync = (input: WorkflowListInput) => {
    const paramenter = jsonToUrlParam(input);
    const url = getApiUrl(`${API.DEFAULT_URL}/all-with-group`, paramenter);
    return request.get<WorkflowListOutput>(url);
};

// 开始流程
export const startAsync = (input: StartWorkflowInput) => {
    request.put<void>(`${API.DEFAULT_URL}/start`, input);
};

// 我发起的流程列表接口
export const getMyStartWorkflowAsync = (input: WorkflowListInput) => {
    const paramenter = jsonToUrlParam(input);
    const url = getApiUrl(`${API.DEFAULT_URL}/my`, paramenter);
    return request.get<PagedResultDto<MyWorkflowListOutput>>(url);
};

// 获取流程定义
export const getDetailsAsync = (id: string) => request.get<WorkflowDto>(`${API.DEFAULT_URL}/details/${id}`);

// 获取流程审核结果
export const getAuditRecordsAsync = (id: string) => request.get<WorkflowAuditDto>(`${API.WORKFLOW_AUDIT_URL}/${id}`);

// 审核
export const auditAsync = (input: WorkflowAuditInput) => {
    return request.put<void>(API.WORKFLOW_AUDIT_URL, input);
};

// 获审核流程列表
export const getListAsync = (input: GetMyAuditPageListInput) => {
    const paramenter = jsonToUrlParam(input);
    const url = getApiUrl(`${API.WORKFLOW_AUDIT_URL}/list`, paramenter);
    return request.get<PagedResultDto<GetMyAuditPageListOutput>>(url);
};

// 删除流程定义
export const deleteAsync = (id: string) => request.delete(`${API.DEFAULT_URL}/${id}`);